package com.csst.business.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.csst.business.model.BOnline;
import com.csst.business.model.TCompany;
import com.csst.business.model.TMessageBoard;
import com.csst.business.model.TTrUser;
import com.csst.business.model.TWuser;
import com.csst.business.service.ICompanyService;
import com.csst.business.service.IMessageBoardService;
import com.csst.business.service.IOnlineService;
import com.csst.business.service.IUserService;
import com.csst.business.service.TestService;
import com.csst.popular.api.SnsAPI;
import com.csst.popular.bean.SnsToken;
import com.csst.popular.bean.User;
import com.csst.services.Thread.TokenThread;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class CompanyAction extends ActionSupport{
	private ICompanyService companyService;
	private IOnlineService onlineService;
	private IUserService userService;
	private IMessageBoardService  messageBoardService;
	private Map<TMessageBoard,List<TMessageBoard>> messageMap;
	private List<BOnline> onlinelist;
	private TWuser tuser;
	private TCompany company;
	private String orderNum;
	private boolean flag;
	
	public CompanyAction() {
	}
	
	public String testAction(){
		return "bbb";
	}
	
	public void saveCompany(){
		company = companyService.saveCompany(company);
		try {
			ServletActionContext.getResponse().getWriter().print(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getCompanyData(){
		company = companyService.getCompanyData();
		return "company";
	}
	
	public String getAboutus(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String turnPages ="waboutus";
		if(!"authdeny".equals(code) && code!=null){
			if(request.getSession().getAttribute("userinfo")!=null){
				User user = (User) request.getSession().getAttribute("userinfo");
				System.out.println("openId:" +user.getOpenid());
				company = companyService.getCompanyData();
			}else{
				SnsAPI snsapi = new SnsAPI();
				SnsToken stken = snsapi.oauth2AccessToken(TokenThread.appid, TokenThread.appsecret, code);
				String accessToken = stken.getAccess_token();
				String refreshToken = stken.getRefresh_token();
				String openId =stken.getOpenid();
				
				User user = snsapi.userinfo(accessToken, openId, "zh_CN");
				request.getSession().setAttribute("refreshToken", refreshToken);
				request.getSession().setAttribute("userinfo", user);
				company = companyService.getCompanyData();
			}
		}else{
			if(request.getSession().getAttribute("userinfo")!=null){
				company = companyService.getCompanyData();
			}else{
				turnPages ="error";
			}
		}
		return turnPages;
	}
	public String getHomePage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		System.out.println(code);
		String turnPages ="homePage";
		if(!"authdeny".equals(code) && code!=null){
			if(request.getSession().getAttribute("userinfo")!=null){
				company = companyService.getCompanyData();
			}else{
				SnsAPI snsapi = new SnsAPI();
				SnsToken stken = snsapi.oauth2AccessToken(TokenThread.appid, TokenThread.appsecret, code);
				String accessToken = stken.getAccess_token();
				String refreshToken = stken.getRefresh_token();
				String openId =stken.getOpenid();
				User user = snsapi.userinfo(accessToken, openId, "zh_CN");
				request.getSession().setAttribute("refreshToken", refreshToken);
				request.getSession().setAttribute("userinfo", user);
				company = companyService.getCompanyData();
			}
		}else{
			if(request.getSession().getAttribute("userinfo")!=null){
				company = companyService.getCompanyData();
			}else{
				turnPages ="error";
			}
		}
		return turnPages;
	}
	
	public String getOrderPage(){
		String turnPages ="orderPage";
		HttpServletRequest request = ServletActionContext.getRequest();
		String openId = (String)request.getSession().getAttribute("openId");
		if(openId==null){
			openId = (String)request.getParameter("openId");
			request.getSession().setAttribute("openId", openId);
		}
		TWuser wuser = (TWuser)request.getSession().getAttribute("tuser");
		if(wuser!=null && wuser.getWNickname()!=null){
			company = companyService.getCompanyData();
			List<BOnline> onlineList = onlineService.getOnlineList(openId);
			orderNum = onlineList.size()+"";
		}else{
			if(openId!=null){
				wuser = userService.getUserByOpenId(openId);
				request.getSession().setAttribute("tuser", wuser);
				if(wuser.getWNickname()!=null){
					company = companyService.getCompanyData();
					List<BOnline> onlineList = onlineService.getOnlineList(openId);
					orderNum = onlineList.size()+"";
				}else{
					turnPages ="error";
				}
			}else{
				turnPages ="error";
			}
			
		}
		
		
		return turnPages;
	}
	
	public String getMessagePage(){
		String turnPages ="messagePage";
		HttpServletRequest request = ServletActionContext.getRequest();
		String openId = (String)request.getSession().getAttribute("openId");
		if(openId==null){
			openId = (String)request.getParameter("openId");
			request.getSession().setAttribute("openId", openId);
		}
		TWuser wuser = (TWuser)request.getSession().getAttribute("tuser");
		if(wuser!=null){
			if(wuser.getWNickname()!=null){
				request.getSession().setAttribute("tuser", wuser);
				messageMap = new LinkedHashMap<TMessageBoard,List<TMessageBoard>>();
				List<TMessageBoard> mList = messageBoardService.getMessageList(0,10);
				if(mList.size()>0){
					for(int i=0;i<mList.size();i++){
						TMessageBoard messageb = (TMessageBoard)mList.get(i);
						if(messageb.getFkMId().equals("0")){
							String msid = messageb.getId();
							List<TMessageBoard> childlist =messageBoardService.getChildMessageByFkId(msid); 
							messageMap.put(messageb, childlist);
						}
					}
				}
			}else{
				if(openId!=null){
					wuser = userService.getUserByOpenId(openId);
					if(wuser.getWNickname()!=null){
						request.getSession().setAttribute("tuser", wuser);
						List<TMessageBoard> mList = messageBoardService.getMessageList(0,10);
						if(mList.size()>0){
							for(int i=0;i<mList.size();i++){
								TMessageBoard messageb = (TMessageBoard)mList.get(i);
								if(messageb.getFkMId().equals("0")){
									String msid = messageb.getId();
									List<TMessageBoard> childlist =messageBoardService.getChildMessageByFkId(msid); 
									messageMap.put(messageb, childlist);
								}
							}
						}
					}else{
						turnPages ="error";
					}
				}else{
					turnPages ="error";
				}
			}
		}else{
			turnPages ="error";
		}
		
		return turnPages;
	}
	
	public String getOrderListPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		System.out.println(code);
		String turnPages ="orderlistPage";
		if(!"authdeny".equals(code) && code!=null){
			if(request.getSession().getAttribute("userinfo")!=null){
				User user = (User) request.getSession().getAttribute("userinfo");
				onlinelist =  onlineService.getOnlineList(user.getOpenid());
				orderNum = onlinelist.size()+"";
			}else{
				SnsAPI snsapi = new SnsAPI();
				SnsToken stken = snsapi.oauth2AccessToken(TokenThread.appid, TokenThread.appsecret, code);
				String accessToken = stken.getAccess_token();
				String refreshToken = stken.getRefresh_token();
				String openId =stken.getOpenid();
				User user = snsapi.userinfo(accessToken, openId, "zh_CN");
				request.getSession().setAttribute("refreshToken", refreshToken);
				request.getSession().setAttribute("userinfo", user);
				onlinelist =  onlineService.getOnlineList(user.getOpenid());
				orderNum = onlinelist.size()+"";
			}
		}else{
			if(request.getSession().getAttribute("userinfo")!=null){
				User user = (User) request.getSession().getAttribute("userinfo");
				onlinelist =  onlineService.getOnlineList(user.getOpenid());
				orderNum = onlinelist.size()+"";
			}else{
				turnPages ="error";
			}
		}
		return turnPages;
	}
	
	public String getAwardGames(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String turnPages ="awardgames";
		if(!"authdeny".equals(code) && code!=null){
			if(request.getSession().getAttribute("userinfo")!=null){
				User user = (User) request.getSession().getAttribute("userinfo");
			}else{
				SnsAPI snsapi = new SnsAPI();
				SnsToken stken = snsapi.oauth2AccessToken(TokenThread.appid, TokenThread.appsecret, code);
				String accessToken = stken.getAccess_token();
				String refreshToken = stken.getRefresh_token();
				String openId =stken.getOpenid();
				User user = snsapi.userinfo(accessToken, openId, "zh_CN");
				request.getSession().setAttribute("refreshToken", refreshToken);
				request.getSession().setAttribute("userinfo", user);
			}
		}else{
			if(request.getSession().getAttribute("userinfo")!=null){
				
			}else{
				turnPages ="error";
			}
		}
		return turnPages;
	}
	public String getGglGames(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String turnPages ="gglgames";
		String openId = (String)request.getSession().getAttribute("openId");
		if(openId==null){
			openId = (String)request.getParameter("openId");
			request.getSession().setAttribute("openId", openId);
		}
		TWuser wuser = (TWuser)request.getSession().getAttribute("tuser");
		if(wuser!=null){
			return turnPages;
		}else{
			if(openId!=null){
				wuser = userService.getUserByOpenId(openId);
				if(wuser.getWNickname()!=null){
					request.getSession().setAttribute("tuser", wuser);
				}else{
					turnPages ="error";
				}
			}else{
				turnPages ="error";
			}
		}
		return turnPages;
	}

	public ICompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(ICompanyService companyService) {
		this.companyService = companyService;
	}

	public TCompany getCompany() {
		return company;
	}

	public void setCompany(TCompany company) {
		this.company = company;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public IOnlineService getOnlineService() {
		return onlineService;
	}

	public void setOnlineService(IOnlineService onlineService) {
		this.onlineService = onlineService;
	}

	public IMessageBoardService getMessageBoardService() {
		return messageBoardService;
	}

	public void setMessageBoardService(IMessageBoardService messageBoardService) {
		this.messageBoardService = messageBoardService;
	}

	public Map<TMessageBoard, List<TMessageBoard>> getMessageMap() {
		return messageMap;
	}

	public void setMessageMap(Map<TMessageBoard, List<TMessageBoard>> messageMap) {
		this.messageMap = messageMap;
	}

	public List<BOnline> getOnlinelist() {
		return onlinelist;
	}

	public void setOnlinelist(List<BOnline> onlinelist) {
		this.onlinelist = onlinelist;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public TWuser getTuser() {
		return tuser;
	}

	public void setTuser(TWuser tuser) {
		this.tuser = tuser;
	}
	
}
